package com.bill99.golden.inf.sso.http;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bill99.golden.inf.sso.mdp.dto.CreateTokenRequestDto;
import com.bill99.golden.inf.sso.mdp.dto.DestroyTokenRequestDto;
import com.bill99.golden.inf.sso.mdp.dto.RefreshTokenTimeRequestDto;
import com.bill99.golden.inf.sso.mdp.dto.ValidateTokenRequestDto;
import com.bill99.golden.inf.sso.mdp.dto.ValidateTokenResponseDto;
import com.bill99.golden.inf.sso.util.CM;

public class SsoServiceHttpImpl {
	Log log = LogFactory.getLog(SsoServiceHttpImpl.class);
	private String ssoURL;

	public SsoServiceHttpImpl(String ssoURL) {
		super();
		this.ssoURL = ssoURL;
	}

	public String createToken(CreateTokenRequestDto r) {
		HttpClient http = new HttpClient();
		// System.out.println("createToken:::" + r);
		try {
			HttpClientParams ps = http.getParams();
			ps.setContentCharset("UTF-8");
			ps.setHttpElementCharset("UTF-8");
			ps.setCredentialCharset("UTF-8");
			PostMethod pm = new PostMethod(this.ssoURL);
			pm.addParameter(new NameValuePair("cmd", "registerSSO"));
			pm.addParameter(new NameValuePair("appCode", CM.stringNoNull(r
					.getAppCode())));
			pm.addParameter(new NameValuePair("userId", CM.stringNoNull(r
					.getUserId())));
			pm.addParameter(new NameValuePair("userIp", CM.stringNoNull(r
					.getUserIp())));
			pm.addParameter(new NameValuePair("explorerType", CM.stringNoNull(r
					.getExplorerType())));
			pm.addParameter(new NameValuePair("userCode", CM.stringNoNull(r
					.getUserCode())));
			pm.addParameter(new NameValuePair("userName", CM.stringNoNull(r
					.getUserName())));
			pm.addParameter(new NameValuePair("orgId", CM.stringNoNull(r
					.getOrgId())));
			pm.addParameter(new NameValuePair("orgCode", CM.stringNoNull(r
					.getOrgCode())));
			pm.addParameter(new NameValuePair("orgName", CM.stringNoNull(r
					.getOrgName())));
			pm.addParameter(new NameValuePair("departmentId", CM.stringNoNull(r
					.getDepartmentId())));
			pm.addParameter(new NameValuePair("departmentCode", CM
					.stringNoNull(r.getDepartmentCode())));
			pm.addParameter(new NameValuePair("departmentName", CM
					.stringNoNull(r.getDepartmentName())));
			pm.addParameter(new NameValuePair("memo", CM.stringNoNull(r
					.getMemo())));

			int status = http.executeMethod(pm);
			if (status == HttpStatus.SC_OK) {
				byte[] data = pm.getResponseBody();
				String token = new String(data);
				log.debug("创建SSO令牌成功：token=" + token);
				return token;
			}
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public ValidateTokenResponseDto validateToken(ValidateTokenRequestDto r) {
		HttpClient http = new HttpClient();
		// System.out.println("validateToken:" + r);
		try {
			HttpClientParams ps = http.getParams();
			ps.setContentCharset("UTF-8");
			ps.setHttpElementCharset("UTF-8");
			ps.setCredentialCharset("UTF-8");
			PostMethod pm = new PostMethod(this.ssoURL);
			pm.addParameter(new NameValuePair("cmd", "validateSSO"));
			pm.addParameter(new NameValuePair("appCode", CM.stringNoNull(r
					.getAppCode())));
			pm.addParameter(new NameValuePair("visitUrl", CM.stringNoNull(r
					.getVisitUrl())));
			pm.addParameter(new NameValuePair("validatePrivilege", ""
					+ r.isValidatePrivilege()));
			pm.addParameter(new NameValuePair("token", CM.stringNoNull(r
					.getToken())));
			pm.addParameter(new NameValuePair("userIp", CM.stringNoNull(r
					.getUserIp())));
			pm.addParameter(new NameValuePair("explorerType", CM.stringNoNull(r
					.getExplorerType())));
			int status = http.executeMethod(pm);
			if (status == HttpStatus.SC_OK) {
				byte[] data = pm.getResponseBody();
				String resStr = new String(data, "UTF-8");
				System.out.println(resStr);
				String t[] = CM.splitString(resStr, "!@!", true);
				if (t != null && t.length > 12) {
					ValidateTokenResponseDto rt = new ValidateTokenResponseDto();
					rt.setValid("true".equalsIgnoreCase(t[0]));
					rt.setErrorCode(t[1]);
					rt.setToken(t[2]);
					rt.setUserId(t[3]);
					rt.setUserCode(t[4]);
					rt.setUserName(t[5]);
					rt.setOrgId(t[6]);
					rt.setOrgCode(t[7]);
					rt.setOrgName(t[8]);
					rt.setDepartmentId(t[9]);
					rt.setDepartmentCode(t[10]);
					rt.setDepartmentName(t[11]);
					rt.setMemo(t[12]);
					rt.setCheckCode(t[13]);
					return rt;
				}
			}
			return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public void destroyToken(DestroyTokenRequestDto r) {
		HttpClient http = new HttpClient();
		try {
			HttpClientParams ps = http.getParams();
			ps.setContentCharset("UTF-8");
			ps.setHttpElementCharset("UTF-8");
			ps.setCredentialCharset("UTF-8");
			PostMethod pm = new PostMethod(this.ssoURL);
			pm.addParameter(new NameValuePair("cmd", "destroySSO"));
			pm.addParameter(new NameValuePair("appCode", CM.stringNoNull(r
					.getAppCode())));
			pm.addParameter(new NameValuePair("token", CM.stringNoNull(r
					.getToken())));
			pm.addParameter(new NameValuePair("userIp", CM.stringNoNull(r
					.getUserIp())));
			pm.addParameter(new NameValuePair("explorerType", CM.stringNoNull(r
					.getExplorerType())));
			int status = http.executeMethod(pm);
			if (status == HttpStatus.SC_OK) {
				log.info("销毁SSO成功！ " + r);
			} else {
				log.info("销毁SSO失败！ " + r);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void refreshToken(RefreshTokenTimeRequestDto r) {
		HttpClient http = new HttpClient();
		try {
			HttpClientParams ps = http.getParams();
			ps.setContentCharset("UTF-8");
			ps.setHttpElementCharset("UTF-8");
			ps.setCredentialCharset("UTF-8");
			PostMethod pm = new PostMethod(this.ssoURL);
			pm.addParameter(new NameValuePair("cmd", "refreshSSO"));
			pm.addParameter(new NameValuePair("appCode", CM.stringNoNull(r
					.getAppCode())));
			pm.addParameter(new NameValuePair("token", CM.stringNoNull(r
					.getToken())));
			pm.addParameter(new NameValuePair("userIp", CM.stringNoNull(r
					.getUserIp())));
			pm.addParameter(new NameValuePair("explorerType", CM.stringNoNull(r
					.getExplorerType())));
			pm.addParameter(new NameValuePair("timeSpan", ""
					+ r.getVisitTimeSpan()));
			int status = http.executeMethod(pm);
			if (status == HttpStatus.SC_OK) {
				log.info("刷新SSO成功！ " + r);
			} else {
				log.info("刷新SSO失败！ " + r);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
